﻿using System;
using Glue;
using Glue.Converters;
using Xunit;

namespace GlueTests.Converters
{
    public class When_custom_converting_between_collections
    {
        private readonly Mapping<DomainPerson, GuiPerson> mapping;

        private class GuiPerson
        {
            public String AStringMappableToAnArray { get; set; }
        }

        private class DomainPerson
        {
            public int[] AnArray { get; set; }
        }

        public When_custom_converting_between_collections()
        {
            mapping = new Mapping<DomainPerson, GuiPerson>();
            
            mapping.Relate(a => a.AnArray, b => b.AStringMappableToAnArray, new QuickConverter<string, int[]>(
                    fromString => new int[] { 1, 2, 3 },
                    fromIntArray => "1,2,3"
                ));
        }

        [Fact]
        public void Should_work_towards_collection()
        {
            var guiPerson = new GuiPerson { AStringMappableToAnArray = "1,2,3" };
            var domainPerson = mapping.Map(guiPerson);

            Assert.Equal(3, domainPerson.AnArray.Length);
        }

        [Fact]
        public void Should_work_away_from_collection()
        {
            var domainPerson = new DomainPerson { AnArray = new int[] {1,2,3} };
            var guiPerson = mapping.Map(domainPerson);

            Assert.Equal("1,2,3", guiPerson.AStringMappableToAnArray);

        }

    }
}
